var Globals = {};

Ti.include('objects.js');
Ti.include('functions.js');

functions.copyFiles();

Ti.include('nieuwe_foto.js');
//removed for this increment
//Globals.Settings = functions.parseSettings();

Globals.leerlingenArray = functions.loadLeerlingen();
Globals.fotoArray = functions.loadFotos();


// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#fff');


var homeWindow = Titanium.UI.createWindow({  
    layout: 'vertical',
    backgroundColor:'#fff',
    navBarHidden:true,
    exitOnClose: true
});


homeWindow.orientationModes = [Titanium.UI.PORTRAIT, Titanium.UI.UPSIDE_PORTRAIT];

var scrollView = Ti.UI.createScrollView({
   width: Ti.UI.FILL,
   height: Ti.UI.FILL,
   contentHeight: 'auto',
   contentWidth: 'auto',
   layout: 'vertical' 
});

var header = Ti.UI.createView({
   width: Ti.UI.FILL,
   height: '85dip',
   backgroundImage: '/images/header.png',
   top: 0
});

var title = Ti.UI.createLabel({
    color: '#fff',
    font: {fontSize:'20dip' },
    top: '18dip',
    text: 'Leerling Portfolio'
})

header.add(title);
homeWindow.add(header);

var buttonHolder = Ti.UI.createView({
   top: '10dip',
   width: '300dip',
   height: '300dip' 
    
});

var nieuweFotobtn = Ti.UI.createImageView({
    width: '140dip',
    height: '140dip', 
    left: 0,
    top: 0,
    image: '/images/nieuwe_foto.png'
});


var leerlingenbtn  = Ti.UI.createImageView({
    width: '140dip',
    height: '140dip', 
    right: 0,
    top: 0,
    image: '/images/leerlingen.png'
});


var gallerijbtn  = Ti.UI.createImageView({
    width: '140dip',
    height: '140dip', 
    left: 0,
    bottom: 0,
    image: '/images/gallerij.png'
});


var instellingenbtn  = Ti.UI.createImageView({
    width: '140dip',
    height: '140dip', 
    right: 0,
    bottom: 0,
    image: '/images/instellingen.png'
});


var actInd = Titanium.UI.createActivityIndicator({
        bottom:10, 
        height:50,
        width:50,
        style:Titanium.UI.iPhone.ActivityIndicatorStyle.PLAIN
});

nieuweFotobtn.addEventListener('click', function(e){
    
    Ti.Media.vibrate([0, 35]);
    
    
    if (Ti.Platform.name != 'android'){
  		homeWindow.add(actInd);
	}
    
    actInd.show();
    
    Titanium.Media.showCamera({
        success:function(e)
        {
            if(e.mediaType == Ti.Media.MEDIA_TYPE_PHOTO)
            {  
            	actInd.hide();
                nieuweFoto.build(e.media);
            }
            else
            {
                alert("Geen foto gevonden!");
                actInd.hide();
            }
        },
        cancel:function()
        {
            actInd.hide(); 
            // doe niks --> terug naar hoofdscherm
        },
        error:function(error)
        {
            // create alert
            var a = Titanium.UI.createAlertDialog({title:'Camera'});
    
            // set message
            if (error.code == Titanium.Media.NO_CAMERA)
            {
                a.setMessage("Helaas is er geen camera gevonden op uw telefoon!");
            }
            else
            {
                a.setMessage("Unexpected error: " + error.code);
            }
    
            // show alert
            actInd.hide();
            a.show();
            homeWindow.close();
        },
        //saveToPhotoGallery:true,
        saveToPhotoGallery:false,
        allowEditing:true,
        mediaTypes:[Ti.Media.MEDIA_TYPE_PHOTO]
    });
    
    
    
	
});

leerlingenbtn.addEventListener('click', function(e){
	Ti.include('leerlingen.js');
	Ti.Media.vibrate([0, 35]);
    leerlingen.build();
});

gallerijbtn.addEventListener('click', function(e){
	Ti.include('gallerij.js');
    Ti.Media.vibrate([0, 35]);
    gallerij.build();
});

instellingenbtn.addEventListener('click', function(e){
	Ti.include('instellingen.js');
	Ti.Media.vibrate([0, 35]);
    instellingen.build();
});

buttonHolder.add(nieuweFotobtn);
buttonHolder.add(leerlingenbtn);
buttonHolder.add(gallerijbtn);
buttonHolder.add(instellingenbtn);


homeWindow.add(buttonHolder);


//Dit toevoegen wanneer het er in landscape ook fatsoenlijk moet uitzien.
    //scrollView.add(buttonHolder);
    //homeWindow.add(scrollView);

homeWindow.open();





